class Buffers:
	def __init__(self):
		self.buffers = {}
		self.origins = {}
		self.buffers_index = []
	
	def has_name(self, name):
		return name in self.buffers_index

	def get_id_by_name(self, name):
		if self.has_name(name): 
			return self.buffers_index.index(name)

	def get_name_by_id(self, id):
		return self.buffers_index[id]

	def get_image_by_name(self, name):
		if self.has_name(name):
			return self.buffers[name]
	
	def id_name_pairs(self):
		pairs = []
		for name in self.buffers_index:
			pairs.append((self.get_id_by_name(name), name))
		pairs.sort()
		return pairs

	def add(self, name, image, origin=None):
		i = 1
		new_name = name
		while self.has_name(new_name):
			new_name = name + " " + str(i)
			i += 1

		self.buffers[new_name] = image
		self.origins[new_name] = origin
		self.buffers_index.append((new_name))
		self.buffers_index.sort()

		return new_name

	def remove(self, name):
		if self.has_name(name):
			del self.buffers[name]
			self.buffers_index.remove(name)

	def get_origin_name(self, name):
		return self.origins[name]

buffers = Buffers()
